#include "FileHandler.h"

FileHandler::FileHandler(void)
{
	m_Position = 0;
}

FileHandler::~FileHandler(void)
{
	Close();
}

void FileHandler::LoadFile( char* a_FilePath, char* a_Mode )
{
	m_File = fopen( a_FilePath, a_Mode );
	m_FileMode = a_Mode;
}

void FileHandler::Close()
{
	fclose( m_File );
	m_Position = 0;
	m_FileMode = NULL;
	m_File = NULL;
}

#pragma region Short Ushort
void FileHandler::ReadShort( short* a_Data )
{
	fread( a_Data, sizeof ( short ), 1, m_File );
	m_Position += sizeof( short );
}

void FileHandler::ReadShort( short* a_Data, unsigned int a_Length )
{
	fread( a_Data, sizeof( short ) + a_Length, 1, m_File );
	m_Position += sizeof( short ) + a_Length;
}

void FileHandler::WriteShort( short* a_Data )
{
	fwrite( a_Data, sizeof( short ), 1, m_File );	
};

void FileHandler::WriteShort( short* a_Data, unsigned int a_Length )
{
	fwrite( a_Data, sizeof( short ) + a_Length, 1, m_File );
};

void FileHandler::ReadUshort( unsigned short* a_Data )
{
	fread( a_Data, sizeof( unsigned short ), 1, m_File );
	m_Position += sizeof( unsigned short );
}

void FileHandler::ReadUshort( unsigned short* a_Data, unsigned int a_Length )
{
	fread( a_Data, sizeof( unsigned short ) + a_Length, 1, m_File );
	m_Position += sizeof( unsigned short ) + a_Length;
}

void FileHandler::WriteUshort( unsigned short* a_Data )
{
	fwrite( a_Data, sizeof( unsigned short ), 1, m_File );
}

void FileHandler::WriteUshort( unsigned short* a_Data, unsigned int a_Length )
{
	fwrite( a_Data, sizeof( unsigned short ) + a_Length, 1, m_File );
}
#pragma endregion Short Ushort

#pragma region Int Uint
void FileHandler::ReadInt( int* a_Data )
{
	fread( a_Data, sizeof( int ), 1, m_File );
	m_Position += sizeof( int );
}

void FileHandler::ReadInt( int* a_Data, unsigned int a_Length )
{
	fread( a_Data, sizeof( int ) + a_Length, 1, m_File );
	m_Position += sizeof( int ) + a_Length;
}

void FileHandler::WriteInt( int* a_Data )
{
	fwrite( a_Data, sizeof( int ), 1, m_File );
}

void FileHandler::WriteInt( int* a_Data, unsigned int a_Length )
{
	fwrite( a_Data, sizeof( int ) + a_Length, 1, m_File);
}

void FileHandler::ReadUint( unsigned int* a_Data )
{
	fread( a_Data, sizeof( unsigned int ), 1, m_File );
}

void FileHandler::ReadUint( unsigned int* a_Data, unsigned int a_Length )
{
	fread( a_Data, sizeof( unsigned int ) + a_Length, 1, m_File );
}

void FileHandler::WriteUint( unsigned int* a_Data )
{
	fwrite( a_Data, sizeof( unsigned int ), 1, m_File );
}

void FileHandler::WriteUint( unsigned int* a_Data, unsigned int a_Length )
{
	fwrite( a_Data, sizeof( unsigned int ) + a_Length, 1, m_File );
}
#pragma endregion Int Uint

#pragma region Float
void FileHandler::ReadFloat( float* a_Data )
{
	fread( a_Data, sizeof ( float ), 1, m_File );
	m_Position += sizeof ( float );
}

void FileHandler::ReadFloat( float* a_Data, unsigned int a_Length )
{
	fread( a_Data, sizeof ( float ) + a_Length, 1, m_File );
	m_Position += sizeof ( float )  + a_Length;
}

void FileHandler::WriteFloat( float* a_Data )
{
	fwrite( a_Data, sizeof ( float ), 1, m_File );
}

void FileHandler::WriteFloat( float* a_Data, unsigned int a_Length )
{
	fwrite( a_Data, sizeof ( float ) + a_Length, 1, m_File );
}
#pragma endregion Float

#pragma region Byte
void FileHandler::ReadByte( unsigned char* a_Data )
{
	fread( a_Data, sizeof( unsigned char ), 1, m_File );
}

void FileHandler::ReadByte( unsigned char* a_Data, unsigned int a_Length )
{
	fread( a_Data, sizeof( unsigned char ) + a_Length, 1, m_File );
}

void FileHandler::WriteByte( unsigned char* a_Data )
{
	fwrite( a_Data, sizeof( unsigned char ), 1, m_File );
}

void FileHandler::WriteByte( unsigned char* a_Data, unsigned int a_Length )
{
	fwrite( a_Data, sizeof( unsigned char ) + a_Length, 1, m_File );
}
#pragma endregion Byte

#pragma region LSTR
void FileHandler::ReadLSTR( char* a_Data )
{
	int Length;
	fread( &Length, sizeof( Length ), 1, m_File );
	a_Data = (char*) malloc ( sizeof( unsigned char ) * Length );

	//const int StrLength = Length;
	//char Return[ StrLength ];
	fread( a_Data, Length, 1, m_File );

	bool check = true;
	//a_Data = &Return;
}

void FileHandler::WriteLSTR()
{

}
#pragma endregion LSTR

#pragma region FSTR
void FileHandler::ReadFSTR( char* a_Data, unsigned int a_Length )
{
	//a_Data = (char*) new char[ a_Length ];
	fread( a_Data, a_Length, 1, m_File );
	m_Position += a_Length;
	
}

void FileHandler::WriteFSTR( char* a_Data, unsigned int a_Length )
{
	fwrite( a_Data, a_Length, 1, m_File );
}
#pragma endregion FSTR

#pragma region SSTR
void FileHandler::ReadSSTR( char* a_Data )
{
	//String with the size stored in a short
	a_Data = new char[255];
	short Length;
	fread( &Length, sizeof( short ), 1, m_File );
	fread( a_Data, Length, 1, m_File );
	m_Position += sizeof( short );
}

void FileHandler::WriteSSTR( char* a_Data )
{
	fwrite( a_Data, sizeof( a_Data ), 1, m_File );

}
#pragma endregion SSTR

#pragma region BSTR
void FileHandler::ReadBSTR( char* a_Data )
{
	unsigned int Length;
	unsigned char ExtraLength;
	ReadByte( &ExtraLength );
	Length = ExtraLength & 0x7F;

	if ( ExtraLength > 0x80 )
	{
		ReadByte( &ExtraLength );
		Length |= ( ExtraLength & 0x7F ) << 7;
	}
	
	//char string[255];
	fread( a_Data, Length, 1, m_File );
	
	m_Position += Length;
}

void FileHandler::WriteBSTR()
{

}

#pragma endregion BSTR


#pragma region Template reading / writing
//template < class T >
//void FileHandler<T>::Read( T* a_Data )
//{
//	fread( &a_Data, sizeof( a_Data ), 1, m_File );
//	m_Position += sizeof( a_Data );
//}
//
//template < class T >
//void FileHandler<T>::Read( T* a_Data , unsigned int a_Length )
//{
//	fread( &a_Data, sizeof( a_Data ) + a_Length, 1, m_File );
//	m_Position += sizeof( a_Data ) + a_Length;
//}
//
//
//template < class T >
//void FileHandler<T>::Write( T* a_Data )
//{
//	fwrite( &a_Data, sizeof( a_Data ), 1, m_File );
//}
//
//
//template < class T >
//void FileHandler<T>::Write( T* a_Data , unsigned int a_Length )
//{
//	fwrite ( &a_Data, sizeof( a_Data ) + a_Length, 1, m_File );
//}
#pragma endregion Template reading / writing